import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';

import 'jzx_app_bar_back_button.dart';

class JzxAAppBar extends StatelessWidget implements PreferredSizeWidget {
  const JzxAAppBar({
    super.key,
    this.leading,
    this.leadingWidth,
    this.title,
    this.titleWidget,
    this.titleWidgetWidth,
    this.backgroundColor,
    this.shadow = false,
    this.systemOverlayStyle,
    this.backIconColor,
    this.actions,
    this.isRootNavigator = false,
    this.centerTitle = true,
    this.bottom,
    this.onWillPop,
  });

  final double? leadingWidth;
  final Widget? leading;
  final String? title;
  final Widget? titleWidget;
  final double? titleWidgetWidth;
  final List<Widget>? actions;
  final Color? backgroundColor;
  final bool shadow;
  final SystemUiOverlayStyle? systemOverlayStyle;
  final Color? backIconColor;
  final bool isRootNavigator;
  final bool centerTitle;
  final PreferredSize? bottom;
  final Future<bool> Function()? onWillPop;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return await onWillPop?.call() ?? true;
      },
      child: Container(
        decoration: BoxDecoration(
          boxShadow: shadow
              ? [
                  BoxShadow(
                    color: const Color(0xFFB3B8CB).withOpacity(0.16),
                    blurRadius: 2,
                    offset: const Offset(0, 1),
                  ),
                ]
              : null,
        ),
        child: AppBar(
          backgroundColor: backgroundColor ?? Colors.transparent,
          titleSpacing: 0,
          leadingWidth: leadingWidth ?? kMinInteractiveDimensionCupertino + 2,
          leading: _buildLeading(context),
          automaticallyImplyLeading: !isRootNavigator,
          centerTitle: centerTitle,
          bottom: bottom,
          title: titleWidget != null
              ? SizedBox(
                  width: titleWidgetWidth,
                  child: titleWidget,
                )
              : (title != null
                  ? Text(
                      title!,
                      textAlign: TextAlign.start,
                      style: GoogleFonts.racingSansOne(
                        fontWeight: FontWeight.normal,
                        fontSize: 21,
                        color: Colors.white,
                      ),
                    ).paddingOnly(left: isRootNavigator ? 12 : 0, top: 4)
                  : null),
          systemOverlayStyle: systemOverlayStyle ?? SystemUiOverlayStyle.light,
          actions: actions,
        ),
      ),
    );
  }

  Widget? _buildLeading(BuildContext context) {
    if (leading != null) return leading;
    if (isRootNavigator) return null;
    final route = ModalRoute.of(context);
    if (route is PageRoute && (route.canPop || route.fullscreenDialog)) {
      return JzxAppBarBackButton(color: backIconColor, superCtx: context);
    }
    return null;
  }

  @override
  Size get preferredSize => Size.fromHeight(
      kMinInteractiveDimensionCupertino + (bottom?.preferredSize.height ?? 0));
}

/// 状态保持
class AutomaticKeepAliveView extends StatefulWidget {
  final Widget child;

  const AutomaticKeepAliveView({
    super.key,
    required this.child,
  });

  @override
  State<AutomaticKeepAliveView> createState() => _AutomaticKeepAliveViewState();
}

class _AutomaticKeepAliveViewState extends State<AutomaticKeepAliveView>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  bool get wantKeepAlive => true;
}
